package com.nbot.server.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.nbot.server.util.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.nbot.server.domain.CctvDO;
import com.nbot.server.service.CctvService;
import com.nbot.server.util.PageUtils;
import com.nbot.server.util.Query;
import com.nbot.server.util.R;
import com.nbot.server.annotation.Log;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * 摄像头状态监控
 *
 * @author nbot
 * @email nbot@163.com
 * @date 2018-07-19 20:07:17
 */

@Controller
@RequestMapping("/server/cctv")
public class CctvController {
    @Autowired
    private CctvService cctvService;

    @GetMapping()
    @RequiresPermissions("server:cctv:cctv")
    String Cctv() {
        return "server/cctv/cctv" ;
    }

    @ResponseBody
    @Log("查看摄像头状态监控")
    @GetMapping("/list")
    @RequiresPermissions("server:cctv:cctv")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<CctvDO> cctvList = cctvService.list(query);
        int total = cctvService.count(query);
        PageUtils pageUtils = new PageUtils(cctvList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("server:cctv:add")
    String add() {
        return "server/cctv/add" ;
    }

    @GetMapping("/edit/{autoId}")
    @RequiresPermissions("server:cctv:edit")
    String edit(@PathVariable("autoId") Long autoId, Model model) {
            CctvDO cctv = cctvService.get(autoId);
        model.addAttribute("cctv" , cctv);
        return "server/cctv/edit" ;
    }

    /**
     * 保存
     */
    @Log("保存摄像头状态监控")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("server:cctv:add")
    public R save(CctvDO cctv) {
        if (cctvService.save(cctv) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("修改摄像头状态监控")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("server:cctv:edit")
    public R update(CctvDO cctv) {
            cctvService.update(cctv);
        return R.ok();
    }

    /**
     * 删除
     */
    @Log("删除摄像头状态监控")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("server:cctv:remove")
    public R remove( Long autoId) {
        if (cctvService.remove(autoId) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @Log("批量删除摄像头状态监控")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("server:cctv:batchRemove")
    public R remove(@RequestParam("ids[]") Long[]autoIds) {
            cctvService.batchRemove(autoIds);
        return R.ok();
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public R importExcel(@RequestParam(value = "excelFile", required = false) MultipartFile file, HttpServletRequest request) {
        try {
            MultipartRequest multipartRequest=(MultipartRequest) request;
            MultipartFile excelFile=multipartRequest.getFile("excelFile");
            if(excelFile!=null){
                List<List<String>> datas = ExcelUtil.readXls(excelFile.getInputStream());
                //TODO: 读到的数据都在datas里面，根据实际业务逻辑做相应处理
                System.err.println("hello!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                if(datas!=null && datas.size()>0){
                    for(int i=0;i<datas.size();i++)
                    {
                        List<String> datai = datas.get(i);
                        int tempInt;
                        CctvDO cctvDoSave = new CctvDO();
                        CctvDO cctvDoUpdate = new CctvDO();
                        cctvDoSave.setDescription(datai.get(0));//第1列，desc
                        cctvDoSave.setIp(datai.get(1));//第2列，ip

                        Date dateTemp = new Date();
                        DateFormat sdfTemp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        dateTemp = sdfTemp.parse(datai.get(2));
                        cctvDoSave.setCreateTime(dateTemp);//第3列，创建时间
                        dateTemp = sdfTemp.parse(datai.get(3));
                        cctvDoSave.setUpdateTime(dateTemp);//第4列，更新时间


                        cctvDoUpdate = cctvService.getByTag(cctvDoSave.getIp());
                        if (cctvDoUpdate != null) {
                            System.err.println("cctv update!");
                            cctvDoUpdate.setDescription(cctvDoSave.getDescription());
                            cctvDoUpdate.setCreateTime(cctvDoSave.getCreateTime());
                            cctvDoUpdate.setUpdateTime(cctvDoSave.getUpdateTime());
                            cctvService.update(cctvDoUpdate);
                        }
                        else {
                            System.err.println("cctv save!");
                            cctvDoSave.setAutoId((long)(cctvService.getMaxAutoid()+1));
                            cctvService.save(cctvDoSave);
                        }

                    }
                    return R.ok();
                }
                else{
                    return R.error();
                }
            }else{
                return R.error();
            }
        } catch (Exception e) {
            return R.error(-1,e.getMessage());
        }
    }

}